/**
 *  Copyright (C) 2013  Piotr Szczepa�ski
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package edu.pw.elka.gtsna.graph_creator;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;


/**
 * @author Piotr Lech Szczepa�ski
 * @author P.Szczepanski@ii.pw.edu.pl 
 *
 */
abstract class FileReader<T> implements Iterable<T>, Iterator<T> {
	protected BufferedReader reader;
	protected String newLine;
	
	public FileReader(String fileName){
		try {			
			File file = new File(fileName);
			if (!file.exists()){
					throw new IOException("File not found.");
			}
			reader = new BufferedReader(new java.io.FileReader(file));			
		} catch (IOException e) {
			e.printStackTrace();
			try {
				reader.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} 
	}
	
	protected void readLine() { 		
		try {
			newLine = reader.readLine(); 
			if (newLine!=null)
				newLine = newLine.trim();
			
		} catch (IOException e) {
			e.printStackTrace();
			try {
				reader.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} 
	}
	
	@Override
	public Iterator<T> iterator() {
		return this;
	}
	
	@Override
	public void remove() {
		throw new UnsupportedOperationException();			
	}
	
	@Override
	public boolean hasNext() {
		return newLine!=null;
	}
	
}
